package cn.kgc.mcartoon.service.impl;

import cn.kgc.mcartoon.dto.ResultJson;
import cn.kgc.mcartoon.entity.MmUsersDO;
import cn.kgc.mcartoon.mapper.MmUsersMapper;
import cn.kgc.mcartoon.service.MmUsersService;
import cn.kgc.mcartoon.utils.JWTUtil;
import cn.kgc.mcartoon.utils.MD5Utils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author christopher
 * @since 2021-09-23
 */
@Service
public class MmUsersServiceImpl extends ServiceImpl<MmUsersMapper, MmUsersDO> implements MmUsersService {

    @Autowired
    private MmUsersMapper usersMapper;

    @Autowired
    private JWTUtil jwtUtil;

    public ResultJson reg(MmUsersDO user){
        user.setPassword("123"+user.getPassword()+"abc");
        int n = usersMapper.insert(user);
        if (n > 0){
            return ResultJson.ok();
        }
        return ResultJson.error("用户添加失败");
    }

    @Override
    public ResultJson regMD5(MmUsersDO user) {
        //密码通过MD5工具类加密
        user.setPassword(MD5Utils.MD5(user.getPassword()));

        int n = usersMapper.insert(user);
        if (n > 0){
            return ResultJson.ok();
        }
        return ResultJson.error("用户添加失败");
    }

    @Override
    public ResultJson login(MmUsersDO user) {

        user.setPassword(MD5Utils.MD5(user.getPassword()));

        QueryWrapper<MmUsersDO> usersDOQueryWrapper = new QueryWrapper<>(user);

        MmUsersDO loginUser = usersMapper.selectOne(usersDOQueryWrapper);

        if(null != loginUser){
            return ResultJson.ok(loginUser);
        }

        return ResultJson.error("用户名密码错");
    }

    @Override
    public ResultJson login2(MmUsersDO user) {

        user.setPassword(MD5Utils.MD5(user.getPassword()));

        QueryWrapper<MmUsersDO> usersDOQueryWrapper = new QueryWrapper<>(new MmUsersDO()).eq("username",user.getUsername());

        MmUsersDO loginUser = usersMapper.selectOne(usersDOQueryWrapper);

        if(null != loginUser){
            if(loginUser.getPassword().equals(user.getPassword())){
                return ResultJson.ok(loginUser);
            }else {
                return ResultJson.error("用户名密码错");
            }

        }

       return ResultJson.error("没有这个用户");
    }

    @Override
    public ResultJson loginUser(MmUsersDO user) {

        user.setPassword(MD5Utils.MD5(user.getPassword()));

        QueryWrapper<MmUsersDO> usersDOQueryWrapper = new QueryWrapper<>(user);

        MmUsersDO loginUser = usersMapper.selectOne(usersDOQueryWrapper);

        if (null == loginUser){
            return ResultJson.error("用户名密码错");
        }
        //登录成功，生成token--令牌
        String token = jwtUtil.createJWT(loginUser.getUserid().toString(), loginUser.getUsername(), true);


        Map<String,Object> loginMap = new HashMap<>();
        loginMap.put("token",token);

        loginMap.put("user",loginUser);



        return ResultJson.ok(loginMap);
    }

}
